﻿using Max.PH5_2103A.Shopping.DAL;
using Max.PH5_2103A.Shopping.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2103A.Shopping.Repository
{
    /// <summary>
    /// 基本的仓储实现
    /// </summary>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        MyDbContext _db;

        public BaseRepository(MyDbContext db)
        {
            _db = db;
        }

        public void Delete(T entity)
        {
            _db.Set<T>().Remove(entity);
        }

        public void Delete(List<T> list)
        {
            _db.Set<T>().RemoveRange(list); 
        }

        public IQueryable<T> GetAll()
        {
            return _db.Set<T>();
        }

        public T GetFirstById(object id)
        {
            var m = _db.Set<T>().Find(id);
            return m;
        }

        public void Insert(T entity)
        {
            _db.Set<T>().Add(entity);
        }

        public void Insert(List<T> list)
        {
            _db.Set<T>().AddRange(list);
        }

        public void Update(T entity)
        {
            _db.Set<T>().Update(entity);
        }

        public void Update(List<T> list)
        {
            _db.Set<T>().UpdateRange(list);
        }
    }
}
